package view 
{
	import core.ViewBase;
	import data.Chapter;
	import data.Lesson;
	import event.MenuEvent;
	import flash.display.MovieClip;
	import flash.events.DRMCustomProperties;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author liwei
	 */
	public class MenuView extends ViewBase 
	{
		[ui]public var exitmc:MovieClip;
		public function MenuView() 
		{
			super();
			load("swf/menu.swf");
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
		}
		override protected function loadInit():void 
		{
			exitmc.buttonMode = true;
			
		}
		private function addedToStage(e:Event):void 
		{
			var chapters:Array = Chapter.getChapters();
			var len:int = chapters.length;
			var status:String;
			var chapterId:int = State.chapter;
			var currentLesson:Lesson = Chapter.getLessonByFlag(chapterId, State.lesson);
			for (var i:int = 0; i < len; i++) {
				var chapter:Chapter = chapters[i];
				var mc:MovieClip = getUI("mc" + i) as MovieClip;
				if (mc != null) {
					var c:int = chapter.getComplete();
					if (c == 0) {
						mc.gotoAndStop(1);
					}else if (c == chapter.length) {
						mc.gotoAndStop(3);
					}else {
						mc.gotoAndStop(2);
					}
				}
				var llen:int = chapter.length;
				if (llen == 1) {
					if (mc != null) {
						mc.addEventListener(MouseEvent.CLICK, mc_click);
						mc.buttonMode = true;
						if (chapter.id == chapterId) {
							mc.gotoAndStop(4);
						}
					}
				}else {
					for (var j:int = 0; j < llen; j++) {
						var lesson:Lesson = chapter.getLesson(j);
						mc = getUI("mc" + i + "" + j) as MovieClip;
						if (mc == null) continue;
						if (lesson == currentLesson) {
							mc.gotoAndStop(4);
						}else {
							status = lesson.status;
							if (status==Lesson.COMPLETE) {
								mc.gotoAndStop(3);
							}else if (lesson.status == Lesson.INCOMPLETE) {
								mc.gotoAndStop(2);
							}else {
								mc.gotoAndStop(1);
							}
						}
						mc.buttonMode = true;
						mc.addEventListener(MouseEvent.CLICK, mc_click);
					}
				}
			}
		}
		
		private function mc_click(e:MouseEvent):void 
		{
			var mc:MovieClip = e.target as MovieClip;
			var name:String = mc.name;
			var sub:String = name.substr(2);
			var lesson:int = 0;
			var chapter:int = 0;
			if (sub.length == 1) {
				chapter = int(sub);
			}else if (sub.length == 2) {
				chapter = int(sub.substr(0, 1));
				lesson = int(sub.substr(1));
			}
			dispatchEvent(new MenuEvent(chapter, lesson));
		}
		
	}

}